//
//  FTMultiValueCell.m
//  WebPDA
//
//  Created by chen shaomou on 10-8-18.
//  Copyright 2010 TenNights.com. All rights reserved.
//

#import "FTMultiValueCell.h"
//#import "FTMultiValueCellTableViewController.h"


@implementation FTMultiValueCell
@synthesize ftMultiValueCellTableViewController;
@synthesize resultLabel;

//prepareForReuse-->initFTMultiValueCell-->configWithObject-->initFTMultiValueCellTableViewController-->didMoveToWindow
-(id)getValue {
	return ftMultiValueCellTableViewController.defaultValue;
}

- (void) configureWithObject:(id)object preferenceSpecifier:(NSDictionary *)dictionary{

	[super configureWithObject:object preferenceSpecifier:dictionary];
	
	self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	
	self.selectionStyle = UITableViewCellSelectionStyleBlue;
	
	resultLabel.textAlignment = UITextAlignmentRight;
	
	resultLabel.font = [UIFont boldSystemFontOfSize:14];
	
	[ftMultiValueCellTableViewController configWithDictionary:dictionary formBundle:formBundle valueHolder:object];
	
	/*
	 set value from entity
	 */
	//getter selector
	

	/*
	 //do the same thing with the mothod move to windows
	NSString *key = [self keyFromPreferenceSpecifier];
	
	if ([object valueForKey:key] != nil) {
		
		resultLabel.text = [valueHolder valueForKey:key];
	}else {
		
		resultLabel.text = [dictionary objectForKey:@"DefaultValue"];
	}

	*/

	 
}

- (id) initWithBundle:(NSBundle *)bundle valueHolder:(id)object reuseIdentifier:(NSString *)reuseIdentifier{
	
	if(self = [super initWithBundle:bundle valueHolder:object reuseIdentifier:reuseIdentifier]){
		
		UILabel *rlabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 10, 100, 25)];
		
		self.resultLabel = rlabel;
		
		[rlabel release];
		
		[self.contentView addSubview:resultLabel];
		
		FTMultiValueCellTableViewController *cellTableViewController = [[FTMultiValueCellTableViewController alloc] init];
		
		self.ftMultiValueCellTableViewController = cellTableViewController;
		
		[cellTableViewController release];
	}
	
	return self;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void)dealloc {
	[ftMultiValueCellTableViewController release];
	[resultLabel release];
    [super dealloc];
}

- (void) didSelected:(UINavigationController *)navigationController{
	
	//NSLog(@"FTMultiValueCell:didSelected");
	
	[navigationController pushViewController:ftMultiValueCellTableViewController animated:YES];
}

	//initFTMUliValueCell-->configureWithObject-->initFTMultiValueCellTableViewController-->didMoveToWindow
- (void) didMoveToWindow{
	
	//ftMultiValueCellTableViewController.defaultValue has already been reset
	NSString *resultOfSelected = ftMultiValueCellTableViewController.defaultValue;
	
	resultLabel.text =  NSLocalizedStringFromTableInBundle(resultOfSelected, @"Root", formBundle, @"");
	
	//NSLog(@"resultLabel.text:%@",resultLabel.text);
	
	//[valueHolder setValue:resultOfSelected forKey:[preferenceSpecifier objectForKey:@"Key"]];
	
	//NSLog(@"valueHolder.value:%@",[valueHolder valueForKey:[preferenceSpecifier objectForKey:@"Key"]]);
	
}

@end


#pragma mark -
//#import "FTMultiValueCellTableViewController.h"

//=============================================================================================================================
//
//
//above FTMultiValueCell.m
//
//
//
//after FTMultiValueCellTableViewController.m
//
//=============================================================================================================================

@implementation FTMultiValueCellTableViewController
@synthesize titles;
@synthesize values;
@synthesize defaultValue;
@synthesize lastIndexPath;
@synthesize formBundle;


#pragma mark -
#pragma mark Initialization

/*
 - (id)initWithStyle:(UITableViewStyle)style {
 // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
 if ((self = [super initWithStyle:style])) {
 }
 return self;
 }
 */

- (id)init{
	
	self = [super initWithStyle:UITableViewStylePlain];
	
	return self;
		
}

- (void) configWithDictionary:(NSDictionary *)dictionary formBundle:(NSBundle *)superBundle valueHolder:(id)valueHolder{
		
		self.values = [dictionary objectForKey:@"Values"];
		
		self.titles = [dictionary objectForKey:@"Titles"];
		
		if([valueHolder valueForKey:[dictionary objectForKey:@"Key"]] == nil){
			
			self.defaultValue = [dictionary objectForKey:@"DefaultValue"];
		}
		else {
			self.defaultValue = [valueHolder valueForKey:[dictionary objectForKey:@"Key"]];
		}
		
		
		self.formBundle = superBundle;
		
		int position = 0;
		
		for(NSString *titleForDefaultValue in titles){
			
			if([titleForDefaultValue isEqualToString:defaultValue]){
				break;
				
			}
			
			position++;
		}
		
		self.lastIndexPath = [NSIndexPath indexPathForRow:position inSection:0];
	
}

#pragma mark -
#pragma mark View lifecycle

/*
 - (void)viewDidLoad {
 [super viewDidLoad];
 
 // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
 // self.navigationItem.rightBarButtonItem = self.editButtonItem;
 }
 */

/*
 - (void)viewWillAppear:(BOOL)animated {
 [super viewWillAppear:animated];
 }
 */
/*
 - (void)viewDidAppear:(BOOL)animated {
 [super viewDidAppear:animated];
 }
 */
/*
 - (void)viewWillDisappear:(BOOL)animated {
 [super viewWillDisappear:animated];
 }
 */
/*
 - (void)viewDidDisappear:(BOOL)animated {
 [super viewDidDisappear:animated];
 }
 */
/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */


#pragma mark -
#pragma mark Table view data source

/*
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 // Return the number of sections.
 return 1;
 }
 */

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [titles count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *FTMulitValueCellTableViewControllerIdentifier = @"FTMulitValueCellTableViewControllerIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FTMulitValueCellTableViewControllerIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FTMulitValueCellTableViewControllerIdentifier] autorelease];
	}
    
    // Configure the cell...
	
	cell.textLabel.frame = CGRectMake(10, 10, 300, 30);
	cell.textLabel.adjustsFontSizeToFitWidth = YES;
	NSString *textLabelOfCell = [self.titles objectAtIndex:indexPath.row];
	cell.textLabel.text = NSLocalizedStringFromTableInBundle(textLabelOfCell, @"Root",formBundle, @"");
	
	NSUInteger row = [indexPath row];
	NSUInteger oldRow = [lastIndexPath row];
	
	cell.accessoryType = (row == oldRow && lastIndexPath != nil) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
	
    return cell;
}


/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */


/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
 }   
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }   
 }
 */


/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */


/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	int newRow = [indexPath row];
	int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
	
	if(newRow != oldRow){
		UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
		newCell.accessoryType = UITableViewCellAccessoryCheckmark;
		
		UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
		oldCell.accessoryType = UITableViewCellAccessoryNone;
		
		//[lastIndexPath release];
		
		self.lastIndexPath = indexPath;
		
		//[defaultValue release];
		
		//NSString *record = defaultValue;
		
		//NSLog(@"%d",[defaultValue retainCount]);
		
		self.defaultValue = [titles objectAtIndex:indexPath.row];
		
		//NSLog(@"%d",[record retainCount]);
	}
	
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

/*
- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;

 }
*/

- (void)dealloc {
	[defaultValue release];
	[titles release];
	[values release];
	[formBundle release];
	[lastIndexPath release];
    [super dealloc];
}


@end